package com.qf.smsplatform.webmaster.controller;

import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.webmaster.annotations.IgnoreLog;
import com.qf.smsplatform.webmaster.annotations.LogOperationAnno;
import com.qf.smsplatform.webmaster.dto.R;
import com.qf.smsplatform.webmaster.pojo.TAdminUser;
import com.qf.smsplatform.webmaster.pojo.TBlackList;
import com.qf.smsplatform.webmaster.service.BlackListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/black")
@Api(tags = "黑名单管理")
public class BlackListController {


    private BlackListService blackListService;

    @Autowired
    public void setBlackListService(BlackListService blackListService){
        this.blackListService = blackListService;
    }

    @IgnoreLog
    @GetMapping("/blacks/{id}")
    public R ById(@PathVariable Long id) {
        TBlackList tBlackList = blackListService.TBlackListById(id);
        return R.setOk(tBlackList);
    }

    @PostMapping("/black/{mobile}")
    public R addBlack(@PathVariable String mobile, @ApiIgnore HttpSession session) {
        TBlackList tBlackList = new TBlackList();
        tBlackList.setMobile(mobile);
        Object user = session.getAttribute("user");
        if (user != null) {
            Long id = ((TAdminUser) user).getId();//创建者的 id
            Long type = ((TAdminUser) user).getType();//创建者类型
            tBlackList.setCreater(id);
            tBlackList.setOwntype(type);
        }
        blackListService.addBlack(tBlackList);

        return R.setOk();
    }

    @GetMapping("/all")
    @ApiOperation("分页查询所有数据")
    @LogOperationAnno("分页查询黑名单的数据")
    public R findAllBlackByPage(@RequestParam(defaultValue = "1") @ApiParam(name ="pageNum",value = "页码") int pageNum, @RequestParam(defaultValue = "10") @ApiParam(name ="pageSize",value = "每页条数")  int pageSize) {
        PageInfo<TBlackList> pageInfo = blackListService.findAll(pageNum, pageSize);
        return R.setOk(pageInfo);
    }

    @PostMapping("/black")
    public R updateBlackById(@RequestBody TBlackList blackList, HttpSession session){
        Object user =session.getAttribute("user");
        if (user !=null){
            Long id = ((TAdminUser) user).getId();//获取当前登录的用户
            Long type = ((TAdminUser) user).getType();//获取当前用户类型
            blackList.setCreater(id);
            blackList.setOwntype(type);
        }
        blackListService.updateBlack(blackList);
        return R.setOk();
    }

    @DeleteMapping("/black")
    public R deleteByIds(@RequestBody Map map){
        System.err.println(map);
        blackListService.deleteBlackByIds((List<Integer>) map.get("ids"));
        return R.setOk();
    }
}
